package FlowGram.Application.CommandHost.FinchActionListeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import FlowGram.Application.Parser.Actions.Control.While;
import FlowGram.Application.Parser.Actions.Property.IsBlocked;
import FlowGram.Application.Parser.Actions.Property.IsNotBlocked;
import FlowGram.Application.Parser.Utility.ClassFinder;

public class WhileListener extends FinchActionListener{

	JFrame frame;
	While whileAct;
	
	
	public WhileListener(JFrame frame){
		this.frame = frame;
	}
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		whileAct = new While();
		
		Class[] properties = ClassFinder.findProperties();
		String[] propName = new String[properties.length];
		for(int i = 0; i < properties.length; i++){
			propName[i] = properties[i].getName().substring(properties[i].getName().lastIndexOf(".")+1);
		}
		
		String property = (String)JOptionPane.showInputDialog(
		                    frame,
		                    "Choose while (true) condition:\n"
		                    + "Condition = ",
		                    "While Parameters",
		                    JOptionPane.PLAIN_MESSAGE,
		                    null,
		                    propName,
		                    "Select Property");
		if(property == null){return;}
		/*
		 * hard coded converter from class name to instantiate the actual class
		 */
		if(property.equals("IsBlocked")){
			whileAct.setProperty(new IsBlocked());
		}else if(property.equals("IsNotBlocked")){
			whileAct.setProperty(new IsNotBlocked());
		}
		
		addCode(whileAct);
		
		
	}
}